package dao.daocontrollers;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import api.entitites.IMaster;
import api.workerswithdb.IDAOMaster;
import api.workerswithdb.SortMastersBy;

public class DAOMaster extends GenericDAO implements IDAOMaster {

	private static final String ORDER_BY_MASTER = "select number_order from orders join masters using(id_master) where master_name=?";
	private static final String SORT_MASTERS = "select master_name,employment_master from masters order by ?";
	private static final String MASTER_INSERT = "insert into masters(master_name ,employment_master) values(?,?)";
	private static final String READ_MASTER="select master_name,employment_master from masters where master_name=?";
	private static final String ID_MASTER="select id_master from masters where master_name =?";
	private static final String DELETE_MASTER="delete from masters where id_master =?";
	private static final String UPDATE_MASTER="update masters set employment_master=? where id_master=?";
	private Logger log = Logger.getLogger(DAOMaster.class);
	

	
	public Boolean insertMasterToDB(IMaster master) {
		Boolean answer = false;
		try {
			super.insert(MASTER_INSERT,master.getNameMaster(), master.getEmploymentMaster());
			answer = true;
		} catch (SQLException sqle) {
			log.error(DBMessanger.MASTER_EXISTS);
		}
		return answer;
	}

	public IMaster getMasterFromDb(String masterName) {
		ResultSet masterFromDbResult;
		try {
			masterFromDbResult = super.read(READ_MASTER,masterName);
			return EntitiesGenerator.parserMaster(masterFromDbResult);
		} catch (SQLException sqle) {
			log.error(DBMessanger.MASTER_NOT_EXISTS);
			return null;
		}
	}

	 public void updateMaster(Connection connection,Boolean employmentMaster,Integer idMaster){
		 try{
		 super.update(connection,UPDATE_MASTER,employmentMaster, idMaster);
		 }catch(SQLException sqle){
			 log.error(DBMessanger.ERROR);
		 }
		
	}
	
	
	
	public ArrayList<IMaster> SortMasters(SortMastersBy sortMastersBy) {
		PreparedStatement sortMasters=null;
		ResultSet sortMastersResult;
		Connection connection=super.connectorTDB.getConnection();
		try {
			sortMasters = connection.prepareStatement(SORT_MASTERS);
			String sort=sortMastersBy.toString().toLowerCase();
			sortMasters.setString(1,sort);
			sortMastersResult = sortMasters.executeQuery();
			return EntitiesGenerator.parserMasters(sortMastersResult);
		} catch (SQLException sqle) {
			log.error(DBMessanger.ERROR);
			return null;
		}finally{
			try{
				sortMasters.close();
				connection.close();
			}catch(SQLException sqle){
				log.error(DBMessanger.ERROR);

			}
		}

	}

	
	
	
	public Integer getOrderByMaster(String masterName) {
		PreparedStatement getOrderByMaster;
		ResultSet orderByMasterResult;
		Integer numberOrder = 0;
		Connection connection=super.connectorTDB.getConnection();
		try {
			getOrderByMaster = connection.prepareStatement(ORDER_BY_MASTER);
			getOrderByMaster.setString(1, masterName);
			orderByMasterResult = getOrderByMaster.executeQuery();
			while (orderByMasterResult.next()) {
				numberOrder = orderByMasterResult.getInt(1);
			}
			getOrderByMaster.close();
			orderByMasterResult.close();
			connection.close();
		} catch (SQLException sqle) {
			log.error(DBMessanger.ERROR_ORDER_BY_MASTER);
		}
		return numberOrder;

	}
	


	public Integer getIdMaster(String masterName) {
		try {
			Integer idMasterResult = super.getObjectId(ID_MASTER,masterName);
			return idMasterResult;
		} catch (SQLException sqle) {
			log.error(DBMessanger.MASTER_NOT_EXISTS);
			return null;
		}
	}

	public Boolean deleteMaster(String masterName) {
		Boolean answer = false;
		try {
			super.deleteObject(DELETE_MASTER,getIdMaster(masterName));
			answer = true;
		} catch (SQLException sqle) {
			log.error(DBMessanger.ERROR);
		}
		return answer;
	}

}
